from flaskDemo.sql.mysql_db import mysql_db
def getRouteList1():
    with mysql_db() as db:
        sql = f"select * from system_menu where menu_type = 3 and pid = '0'"
        result = db.fetch_all(sql)
        def circulate_child(item):
            # 先获取菜单下是否有查看权限
            # sql = f"select * from system_menu where pid = '{item['id']}' and menu_type = 4 and permissions like '%show'"
            # child_btn = db.fetch_one(sql)

            sql = f"select * from system_menu where pid = '{item['id']}' and menu_type = 3"
            child_result = db.fetch_all(sql)
            if len(child_result) > 0:
                item["children"] = child_result
                for itemChild in child_result:
                    circulate_child(itemChild)
            else:
                item["children"] = []
                return -1

        if len(result) > 0:
            for item in result:
                if item["child_menu_show"] == 1:
                    item["children"] = []
                else:
                    isExist = circulate_child(item)

        print(result)

def getRouteList():
    with mysql_db() as db:
        sql = f"select * from system_menu where menu_type = 3 and pid = '0'"
        result = db.fetch_all(sql)
        def circulate_child(item):
            sql = f"select * from system_menu where pid = '{item['id']}' and menu_type = 3"
            child_result = db.fetch_all(sql)
            if len(child_result) > 0:
                item["children"] = child_result
                for itemChild in child_result:
                    circulate_child(itemChild)
            else:
                item["children"] = []
                return -1

        if len(result) > 0:
            for item in result:
                if item["child_menu_show"] == 1:
                    item["children"] = []
                else:
                    isExist = circulate_child(item)

        print(result)

def getMenuList():
    with mysql_db() as db:
        sql = f"select * from system_menu where menu_type = 3 and pid = '0'"
        result = db.fetch_all(sql)
        def circulate_child(item):
            sql = f"select * from system_menu where pid = '{item['id']}' and menu_type = 3"
            child_result = db.fetch_all(sql)
            if len(child_result) > 0:
                item["children"] = child_result
                for itemChild in child_result:
                    itemChild["children"] = getPermissionList(itemChild)
                    circulate_child(itemChild)
            else:
                item["children"] = getPermissionList(item)
                return -1

        if len(result) > 0:
            for item in result:
                item["children"] = getPermissionList(item)
                circulate_child(item)

        print(result)

def getPermissionList(item):
    with mysql_db() as db:
        sql = f"select * from system_menu where pid = '{item['id']}' and menu_type = 4"
        btn_result = db.fetch_all(sql)
        return btn_result


def getPermissionedList(roleId,pid):
    with mysql_db() as db:
        sql = f"select a.role_id,GROUP_CONCAT(a.menu_id) as menuId from system_role_menu a left join system_menu b on a.menu_id = b.id  group by b.pid HAVING a.role_id = '{roleId}' and b.pid = '{pid}'"
        result = db.fetch_one(sql)
        if len(result) > 0:
            idArr = result["menuId"].split(",")
            return idArr
        else:
            return []

"""
 menu_type = 3
        roleIds = "1,2"
        sql = f"select DISTINCT a.menu_id,a.role_id,b.permissions,b.pid  from system_role_menu a left join system_menu b on a.menu_id = b.id  where FIND_IN_SET(a.role_id,'{roleIds}') and b.permissions like '%%show'"
        result = db.fetch_all(sql)

        def getPermissionMenu(item, menu_type, flag):
            menuSql = f"select * from system_menu where id = '{item['pid']}' and menu_type = {menu_type}"
            menuResult = db.fetch_one(menuSql)
            if flag != 1:
                menuResult["children"] = [item]
            if menuResult['pid'] != '0':
                return getPermissionMenu(menuResult, menu_type, 2)
            else:
                return menuResult
            
        for item in result:
            menuResult = getPermissionMenu(item, menu_type, 1)
"""
def getPermissionRoutes():
    with mysql_db() as db:
        sql = f"select * from system_menu where menu_type = 3 and pid = '0'"
        result = db.fetch_all(sql)
        def circulate_child(item, pidArr):
            sql = f"select * from system_menu where pid = '{item['id']}' and menu_type = 3"
            child_result = db.fetch_all(sql)
            if len(child_result) > 0:
                item["children"] = list(filter(lambda n: n["id"] in pidArr, child_result))
                for itemChild in child_result:
                    circulate_child(itemChild, pidArr)
            else:
                item["children"] = []

        if len(result) > 0:
            pidArr = getIsShowMenu()
            newList = list(filter(lambda n: n["id"] in pidArr, result))
            for item in newList:
                if item["child_menu_show"] == 1:
                    item["children"] = []
                else:
                    circulate_child(item, pidArr)
            print(newList)
def getIsShowMenu():
    roleIds = '1,2'
    with mysql_db() as db:
        sql = f"select DISTINCT a.menu_id,a.role_id,b.permissions,b.id_path  from system_role_menu a left join system_menu b on a.menu_id = b.id  where FIND_IN_SET(a.role_id,'{roleIds}') and b.permissions like '%%show'"
        result = db.fetch_all(sql)
        pidArr = map(lambda n: n["id_path"].split(","), result)
        pidList = set(list(sum(list(pidArr), [])))
        return pidList


def getDepartList():
    with mysql_db() as db:
        sql = f"select * from system_depart where pid = '0'"
        result = db.fetch_all(sql)
        def circulate_child(item):
            sql = f"select * from system_depart where pid = '{item['id']}'"
            child_result = db.fetch_all(sql)
            item["children"] = child_result
            if len(child_result) > 0:
                for itemChild in child_result:
                    circulate_child(itemChild)
            else:
                item["children"] = []
                return -1

        if len(result) > 0:
            for item in result:
                circulate_child(item)

        print(result)

def getDepartChildrenList(item):
    with mysql_db() as db:
        sql = f"select * from system_depart where pid = '{item['id']}'"
        btn_result = db.fetch_all(sql)
        return btn_result

if __name__ == "__main__":
    getDepartList()
    # getIsShowMenu()